package com.neusoft.neuiotms.dcom.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.auth.common.bean.UserBean;
import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.TeamGroupDTO;
import com.neusoft.neuiotms.dcom.dto.UserDutyInfoDTO;
import com.neusoft.neuiotms.dcom.model.FunctionalGroup;
import com.neusoft.neuiotms.dcom.model.TeamGroup;
import com.neusoft.neuiotms.dcom.model.TeamUser;
import com.neusoft.neuiotms.dcom.repository.FunctionalGroupRepository;
import com.neusoft.neuiotms.dcom.repository.TeamGroupRepository;
import com.neusoft.neuiotms.dcom.repository.TeamUserRepository;

@Component
public class TeamGroupConverter extends AbstractConverter<TeamGroup, TeamGroupDTO> {

    @Autowired
    private TeamUserConverter teamUserConverter;
    @Autowired
    private TeamGroupRepository teamGroupRepository;
    @Autowired
    private FunctionalGroupRepository functionalGroupRepository;
    @Autowired
    private TeamUserRepository teamUserRepository;

    @Override
    public TeamGroup toModel(final TeamGroupDTO dto) {

        TeamGroup model;

        if (dto.isNew()) {
            model = new TeamGroup();
        } else {
            model = this.teamGroupRepository.getOne(dto.getId());
        }

        model.setName(dto.getName());
        model.setOrgCode(dto.getOrgCode());
        model.setOrgName(dto.getOrgName());
        model.setColor(dto.getColor());

        // 移除JPA关系
        final List<TeamUser> users = model.getUsers().stream().collect(Collectors.toList());
        users.forEach(k -> {
            model.getUsers().remove(k);
            this.teamUserRepository.delete(k);
        });

        // 职能
        if (null != dto.getFunctionalGroupId()) {
            final FunctionalGroup functionalGroup = this.functionalGroupRepository.getOne(dto.getFunctionalGroupId());
            model.setFunctionalGroup(functionalGroup);
        }

        // 人员
        dto.getUsers().stream().map(s -> {
            final TeamUser userModel = this.teamUserConverter.toModel(s);
            userModel.setGroup(model);
            return userModel;
        }).filter(k -> null != k).forEach(model.getUsers()::add);

        return model;
    }

    @Override
    public TeamGroupDTO toDTO(final TeamGroup model, final boolean forListView) {

        final TeamGroupDTO dto = new TeamGroupDTO();
        dto.setId(model.getId());
        dto.setName(model.getName());
        dto.setOrgCode(model.getOrgCode());
        dto.setOrgName(model.getOrgName());
        dto.setColor(model.getColor());

        if (null != model.getFunctionalGroup()) {
            dto.setFunctionalGroupId(model.getFunctionalGroup().getId());
            dto.setFunctionalGroupName(model.getFunctionalGroup().getName());
        }

        if (!forListView) {
            model.getUsers().stream().map(s -> this.teamUserConverter.toDTO(s)).filter(k -> null != k)
                    .forEach(dto.getUsers()::add);
        }

        return dto;
    }

    public List<UserDutyInfoDTO> toListBean(final List<TeamGroup> models, final UserBean user) {

        final List<UserDutyInfoDTO> result = new ArrayList<>();

        models.stream().map(k -> {
            final UserDutyInfoDTO dto = new UserDutyInfoDTO();
            dto.setName(user.getName());
            dto.setUserName(user.getUsername());
            dto.setOrgCode(k.getOrgCode());
            dto.setOrgName(k.getOrgName());
            dto.setTeamGroupId(k.getId());
            dto.setTeamGroupName(k.getName());
            dto.setFunctionalGroupId(k.getFunctionalGroup().getId());
            dto.setFunctionalGroupName(k.getFunctionalGroup().getName());
            return dto;
        }).forEach(result::add);

        return result;
    }
}
